#include "../head/guideform.h"
#include "ui_guideform.h"
#include <QtWidgets/QApplication>
#include <QtCore/QDir>
#include <QtQuick/QQuickView>
#include <QtQml/QQmlEngine>
#include <QtCharts>
#include <QtCharts/QChartView>
#include <QtCharts/QChart>
#include <QtCharts/QLegend>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCore/QTime>
#include <QtWidgets/QGridLayout>
#include <QtCharts/QScatterSeries>



GuideForm::GuideForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::GuideForm)
{
    ui->setupUi(this);
    //绘制施工曲线饼图
   drawPieChart();

   //初始化散点图
   series1 = new QScatterSeries();              // 创建一个散点数据集对象
   series1->setMarkerShape(QScatterSeries::MarkerShapeCircle);  // 设置绘制的散点的样式为圆
   series1->setMarkerSize(10);                                  // 设置绘制的点的大小

    //绘制施工曲线点图
   drawLineChart();
}

//void GuideForm::renderData(int i){
//    ui->ssfwj->setText((QString)i);
//}

//绘制施工曲线饼图
void GuideForm::drawPieChart(){


   QSplineSeries *series = new QSplineSeries();
   series->append(0,0);                   // 向series中添加数据，X：角度 Y：到圆心距离
   series->append(75,10);
   QSplineSeries *series1 = new QSplineSeries();
   series1->append(0,0);                   // 向series中添加数据，X：角度 Y：到圆心距离
   series1->append(115,10);

   QSplineSeries *series2 = new QSplineSeries();
   series2->append(0,0);                   // 向series中添加数据，X：角度 Y：到圆心距离
   series2->append(90,10);

   QPolarChart* chart = new QPolarChart();           // 创建一个极坐标系图表
   chart->legend()->setVisible(false);               // 隐藏图例
   chart->addSeries(series);                         // 将创建的series添加进图表中
   chart->addSeries(series1);
   chart->addSeries(series2);

   QValueAxis* angularAxis = new QValueAxis();
   //第一个和最后一个间隔位于0/360度角
   angularAxis->setTickCount(25);
   angularAxis->setMinorTickCount(15);
//    angularAxis->setGridLineVisible(false);           //大刻度是否可见
   angularAxis->setMinorGridLineVisible(false);        //小刻度是否可见
   angularAxis->setLabelFormat("%d");
   //设置角度坐标系的范围是0~360度
   angularAxis->setRange(0,360);
   //将坐标轴加入到图形中去，注意第二个参数的选择
   chart->addAxis(angularAxis,QPolarChart::PolarOrientationAngular);
   //设置弧长的坐标系设置
   QValueAxis* radialAxis = new QValueAxis();
   radialAxis->setTickCount(5);
   radialAxis->setLabelsVisible(false);
//    radialAxis->setLabelFormat("%ddB");
   radialAxis->setRange(0,10);
   radialAxis->setLineVisible(false);          //轴线是否可见
   //将弧长坐标系加入到图形中去
   chart->addAxis(radialAxis,QPolarChart::PolarOrientationRadial);
   //将曲线和坐标轴绑定起来
   series->attachAxis(angularAxis);
   series->attachAxis(radialAxis);

   series1->attachAxis(angularAxis);
   series1->attachAxis(radialAxis);

   series2->attachAxis(angularAxis);
   series2->attachAxis(radialAxis);

   QChartView *charview = new QChartView(chart);
   charview->setChart(chart);
   //charview->chart()->setTitle("雷达图");
   charview->setRenderHint(QPainter::Antialiasing);   //抗锯齿
   ui->gridLayout_4->addWidget(charview,1,1);
}


void GuideForm::figureDrillPosition (float courseAngle, float pitchAngle, float drillLength) { //courseAngle 航向角， pitchAngle 俯仰角，drillLength 钻杆长度

    x = x + drillLength * sin(courseAngle) * sin(pitchAngle);
    y = y + drillLength * sin(courseAngle) * cos(pitchAngle);
    z = z + drillLength * cos(courseAngle);
    xPos ++;
    series1->append(y,z);


}

//绘制施工曲线点图
void GuideForm::drawLineChart(){
       // scatter chart


//   for(int i = 0; i < 20; i++)    // 添加10个任意点
//   {
//       series1->append(i, qrand() % 100);
//   }

   QChart* chart1 = new QChart();
   chart1->legend()->setVisible(false);               // 隐藏图例

   QValueAxis *axisX, *axisY;
   axisX = new QValueAxis();
   axisY = new QValueAxis();
//  m_axisX->setTitleText("X-label");
//  m_axisY->setTitleText("Y-label");
   axisX->setMin(0);
   axisY->setMin(0);
   axisX->setMax(100);
   axisY->setMax(20);
   axisY->setReverse(true);                      //坐标翻转
   axisX->setLabelFormat("%d");
   axisY->setLabelFormat("%0.1f");
   chart1->addAxis(axisY, Qt::AlignLeft);                      // 将X轴添加到图表上
   chart1->addAxis(axisX, Qt::AlignTop);                    // 将Y轴添加到图表上
   chart1->addSeries(series1);                              // 将曲线对象添加到图表上

   series1->attachAxis(axisX);                             // 曲线对象关联上X轴，此步骤必须在addSeries之后
   series1->attachAxis(axisY);                             // 曲线对象关联上Y轴，此步骤必须在addSeries之后
   QChartView *chartView = new QChartView(chart1);
   ui->gridLayoutLine->addWidget(chartView, 1, 1);


}

GuideForm::~GuideForm()
{
    delete ui;
}

void GuideForm::on_pushButton_2_clicked()
{
    figureDrillPosition( ui->fwj->text().toFloat(),ui->qj->text().toFloat(),ui->zgcd->text().toFloat());

    drawLineChart();

//    DBSql dbsql;
//    QString ssfwj = ui->ssfwj->text();
//    QDateTime current_date_time =QDateTime::currentDateTime();
//    QString current_date =current_date_time.toString("yyyy.MM.dd hh:mm:ss");
//    QUuid uuid = QUuid::createUuid();
//    QString sqlinsert = "insert into t_detect_main (detectId,ssfxj,recordtime) values "
//                        "('"+uuid.toString()+"','"+ssfwj+"','"+current_date+"')";
//    dbsql.sqlInsert(sqlinsert);

}
